package com.wpp.util;

import com.wpp.service.Studentservice;

import java.io.InputStream;
import java.util.Properties;

public class FactoryMode {


    //第一种方式解耦
    public static Studentservice getStudentservice() {
        Studentservice instance = null;
        try {
            //使用反射创建对象
            Class clazz = Class.forName("com.wpp.service.impl.StudentserviceImpl");
            instance = (Studentservice) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }


    //第二种方式解耦,读取配置文件,使用properties方法
    static Properties properties = null;

    static {
        properties = new Properties();
        try {
            InputStream resourceAsStream = FactoryMode.class.getClassLoader().getResourceAsStream("Factory.properties");
            properties.load(resourceAsStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Object getBean(String key) {
        Object instance = null;
        try {
            System.out.println(properties.getProperty(key));
            Class clazz = Class.forName(properties.getProperty(key));
            instance = (Object) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }


}
